/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prft.rms.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.prft.rms.dao.EmployeeDao;
import com.prft.rms.entity.Employee;
import com.prft.rms.orm.Page;

/**
 * 
 * @author cairns.ma
 */
@Service
@Transactional
public class EmployeeService {

	Logger logger = LoggerFactory.getLogger(EmployeeService.class);

	@Autowired
	private EmployeeDao employeeDao;

	@Profiled
	@Transactional(readOnly = true)
	public List<Employee> getAllEmployee() {
		List<Employee> list = employeeDao.getAll();
		return list;
	}

	@Profiled
	@Transactional(readOnly = true)
	public List<Employee> getSortedEmployees(String sort, boolean descend, final Criterion... criterions) {
		Criteria criteria = employeeDao.createCriteria(criterions);

		if (descend) {
			criteria.addOrder(Order.desc(sort));
		} else {
			criteria.addOrder(Order.asc(sort));
		}
		List<Employee> list = criteria.list();
		return list;
	}

	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Employee getEmployee(Integer id) {
		logger.debug("get begin");
		Employee employee = employeeDao.get(id);
		logger.debug("get end");
		return employee;
	}

	public void saveEmployee(Employee employee) {
		employeeDao.save(employee);
	}

	public Page<Employee> getPage(Page<Employee> page) {
		page = employeeDao.getAll(page);
		return page;
	}

}
